// src/store/index.ts
import { configureStore } from "@reduxjs/toolkit";
import { useDispatch, TypedUseSelectorHook, useSelector } from "react-redux";
import {authReducer, authSlice} from "@store/slices/authSlice";
import {apiSlice} from "@store/apiSlice";
import { persistStore } from "redux-persist";
import {articleSlice} from "@store/slices/articleSlice";
import {doctorSlice} from "@store/slices/doctorSlice";
import {consultReducer, consultSlice} from "@store/slices/consultSlice";

// 创建 store 对象
const store = configureStore({
    reducer: {
        [authSlice.name]: authReducer,
        [apiSlice.reducerPath]: apiSlice.reducer,
        [articleSlice.name]: articleSlice.reducer,
        [doctorSlice.name]: doctorSlice.reducer,
        [consultSlice.name]: consultReducer
    },
    middleware: (getDefaultMiddleware) =>
        getDefaultMiddleware({ serializableCheck: false,}).concat([apiSlice.middleware]),
    devTools: process.env.NODE_ENV !== "production",
});

// 获取 dispatch 方法的类型
export type AppDispatch = typeof store.dispatch;
// 用于获取带有类型的 dispatch 方法
export const useTypedDispatch = () => useDispatch<AppDispatch>();
// 获取 store 中管理的状态的类型
export type AppState = ReturnType<typeof store.getState>;
// 用于获取带有类型的 useSelector 方法
export const useTypedSelector: TypedUseSelectorHook<AppState> = useSelector;
// 默认导出 store 对象
export default store;
// 持久化状态对象
export const persistor = persistStore(store);